﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace DevExpressSimple
{
    public partial class IconButton : UserControl
    {
        public IconButton()
        {
            InitializeComponent();
            pictureBox.MouseMove += OnMouseMove;
            pictureBox.MouseLeave += OnMouseLeave;
            pictureBox.Click += (sender,e)=> { OnClick(e); };
        }

        private Image image;

        public Image Image
        {
            get { return image; }
            set
            {
                image = value;
                pictureBox.Image = image;
            }
        }

        public Image HoverImage { get; set; }
        private Color originColor;
        private readonly Color hoverColor = Color.FromArgb(100, 0xCC, 0xCC, 0xCC);
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            originColor = BackColor;
        }


        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            OnMouseLeave(this, e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            OnMouseMove(this, e);
        }


        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            BackColor = hoverColor;
            pictureBox.BackColor = hoverColor;
            if (HoverImage != null) pictureBox.Image = HoverImage;
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            BackColor = originColor;
            pictureBox.BackColor = originColor;
            if (HoverImage != null) pictureBox.Image = Image;
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
        }
    }
}
